package com.wudl.schedule.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author ：wudl
 * @date ：Created in 2022-03-14 17:13
 * @description：token 工具类
 * @modified By：
 * @version: 1.0
 */

@Slf4j
public class TokenUtils {

    /**
     * 获取token的值
     * @return
     */
    public static  String  getToken()
    {
        String url = "http://192.168.1.162:12345/dolphinscheduler/login";
        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.add("userName","hdfs");
        paramsMap.add("userPassword","admin@123");
        log.info("获取登录地址-------------------->{}",url);
        ResponseEntity<Result> loginResult = RestTemplateUtils.post(url, paramsMap, Result.class);
        log.info("请求结果------------------------{}",loginResult.getBody().getMsg());
        return  loginResult.getHeaders().get("Set-Cookie").get(0);
    }

    /**
     *  通用的HttpHeaders
     * @return
     */
    public static HttpHeaders  getHttpHeaders()
    {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Vary", "Accept-Encoding, User-Agent");
        requestHeaders.add("Transfer-Encoding", "chunked");
        requestHeaders.add("Content-Type", "application/json;charset=utf-8");
        requestHeaders.add(HttpHeaders.COOKIE, getToken());
        return requestHeaders;
    }

}
